
package it.unibo.deis.lia.filters.graphic;

import java.awt.*;
import java.util.*;

import javax.swing.JPanel;

public class FunctionPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int /*xMin=0, xMax=100,*/ yMin=-100, yMax=-10;
	private int width, height;
	private float /*fattoreScalaX,*/ fattoreScalaY;
	private int dist=5;//10;
	
	private TreeMap<String,Vector> db;
	
	public FunctionPanel(TreeMap<String,Vector> db,int width,int height,int yMin,int yMax){
		this.db=db;
		this.height=height;
		this.width=width;
		this.yMin=yMin;
		this.yMax=yMax;
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		setBackground(Color.white);
		fattoreScalaY=-(float)height/((float)(yMax-yMin));

		g.setColor(Color.blue);
		
		// cornice
		g.drawRect(0,0,width,height);
		// assi cartesiani
		float alt=(float)height;
		float larg=(float)width;
		g.drawLine(0,Math.round(alt/2*1), width,Math.round(alt/2*1)); // x
		
		g.drawLine(0,Math.round(alt/4*1), width,Math.round(alt/4*1)); // x
		g.drawLine(0,Math.round(alt/4*3), width,Math.round(alt/4*3)); // x
		
		g.drawLine(0,Math.round(alt/8*1), width,Math.round(alt/8*1)); // x
		g.drawLine(0,Math.round(alt/8*3), width,Math.round(alt/8*3)); // x
		g.drawLine(0,Math.round(alt/8*5), width,Math.round(alt/8*5)); // x
		g.drawLine(0,Math.round(alt/8*7), width,Math.round(alt/8*7)); // x
		
		
		g.setColor(Color.blue);
		
		g.drawString(""+yMin, Math.round(larg/2),height);
		g.drawString(""+yMax, Math.round(larg/2),15);
		
		g.drawString(""+Math.round((float)(yMin-yMax)/2+yMax), Math.round(larg/2),Math.round(alt/2));
		
		g.drawString(""+Math.round((float)(yMin-yMax)/4*1+(float)yMax), Math.round(larg/2),Math.round(alt/4*1));
		g.drawString(""+Math.round((float)(yMin-yMax)/4*3+(float)yMax), Math.round(larg/2),Math.round(alt/4*3));
		
		g.drawString(""+Math.round((float)(yMin-yMax)/8*1+(float)yMax), Math.round(larg/2),Math.round(alt/8*1));
		g.drawString(""+Math.round((float)(yMin-yMax)/8*3+(float)yMax), Math.round(larg/2),Math.round(alt/8*3));
		g.drawString(""+Math.round((float)(yMin-yMax)/8*5+(float)yMax), Math.round(larg/2),Math.round(alt/8*5));
		g.drawString(""+Math.round((float)(yMin-yMax)/8*7+(float)yMax), Math.round(larg/2),Math.round(alt/8*7));
		
		
		Vector<Color> colori=new Vector<Color>();
		colori.addElement(Color.black);
		colori.addElement(Color.blue);
		colori.addElement(Color.red);
		colori.addElement(Color.gray);
		colori.addElement(Color.green);
		colori.addElement(Color.magenta);
		colori.addElement(Color.orange);
		colori.addElement(Color.cyan);
		colori.addElement(Color.darkGray);
		colori.addElement(Color.lightGray);
		colori.addElement(Color.pink);
		colori.addElement(Color.yellow);
		
		int plus=0;
		int n=0;
		if(db!=null){
			// iteration on available APs
			synchronized(db){
				Iterator<String> it=db.keySet().iterator();
				int contatore=0;
				while(it.hasNext()){
					
					g.setColor((Color)colori.elementAt(n));
					n++;
					if(n==colori.size()) n=0;
					
					String bssid=it.next();
					//g.drawString(bssid, 10,20+20*contatore);// mac in colonna
					int colonna=contatore/3;
					int riga=contatore%3;
					g.drawString(bssid, 20+100*colonna,20+20*riga);
					contatore++;
					
					Vector<Object> v=db.get(bssid);
					if(v.size()*dist-1>width){
						plus=Math.round(((float)v.size()*(float)dist-(float)width)/(float)dist)-1;
					}
					if(plus<0)plus=0;
					int i=plus;
					for(;i<v.size();i++){
						if(v.elementAt(i)!=null&&i==0){
							// first value
							if(v.elementAt(i) instanceof Integer){
								g.fillOval((i-plus)*dist-2, Math.round((Math.abs(((Integer)v.elementAt(i)).intValue())-yMax/*+yMax*/)*fattoreScalaY)-2, 4, 4);
							}
							else if(v.elementAt(i) instanceof Float){
								g.fillOval((i-plus)*dist-2, Math.round((Math.abs(((Float)v.elementAt(i)).floatValue())-yMax/*+yMax*/)*fattoreScalaY)-2, 4, 4);
							}
							else if(v.elementAt(i) instanceof Double){
								g.fillOval((i-plus)*dist-2, (int)Math.round((Math.abs(((Double)v.elementAt(i)).doubleValue())-yMax/*+yMax*/)*fattoreScalaY)-2, 4, 4);
							}
						}
						else if(v.elementAt(i)!=null&&v.elementAt(i-1)!=null){
							// intermediate values
							if(v.elementAt(i) instanceof Integer){
								g.drawLine((i-plus)*dist, Math.round((Math.abs(((Integer)v.elementAt(i)).intValue())-yMax)*fattoreScalaY), (i-1-plus)*dist, Math.round((Math.abs(((Integer)v.elementAt(i-1)).intValue())-yMax/*+yMax*/)*fattoreScalaY));
							}
							else if(v.elementAt(i) instanceof Double){
								g.drawLine((i-plus)*dist, (int)Math.round((Math.abs(((Double)v.elementAt(i)).doubleValue())-yMax)*fattoreScalaY), (i-1-plus)*dist, (int)Math.round((Math.abs(((Double)v.elementAt(i-1)).doubleValue())-yMax/*+yMax*/)*fattoreScalaY));
							}
							else if(v.elementAt(i) instanceof Float){
								g.drawLine((i-plus)*dist, (int)Math.round((Math.abs(((Float)v.elementAt(i)).doubleValue())-yMax)*fattoreScalaY), (i-1-plus)*dist, Math.round((Math.abs(((Float)v.elementAt(i-1)).floatValue())-yMax/*+yMax*/)*fattoreScalaY));
							}
						}
						else if(v.elementAt(i)!=null&&v.elementAt(i-1)==null){
							// isolated values
							if(v.elementAt(i) instanceof Integer){
								g.fillOval((i-plus)*dist-2, Math.round((Math.abs(((Integer)v.elementAt(i)).intValue())-yMax/*+yMax*/)*fattoreScalaY)-2, 4, 4);
							}
							else if(v.elementAt(i) instanceof Float){
								g.fillOval((i-plus)*dist-2, Math.round((Math.abs(((Float)v.elementAt(i)).intValue())-yMax/*+yMax*/)*fattoreScalaY)-2, 4, 4);
							}
							else if(v.elementAt(i) instanceof Double){
								g.fillOval((i-plus)*dist-2, (int)Math.round((Math.abs(((Double)v.elementAt(i)).doubleValue())-yMax/*+yMax*/)*fattoreScalaY)-2, 4, 4);
							}
						}
					}
				}
			}
		}
	}
}